package com.ruoyi.factory.costControl.common.utils;

import com.ruoyi.factory.qylock.utils.OkHttpUtils;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class FkAbUtils {

    public static void handleParam(){
        //        key：
//        测试环境：
//        请求地址：http://fee-t.vetech.cn/fcopen/fcoutapi
//        compid：SYZLCLY
//        account：RICHFUL
//        秘钥：d597c54a85334f9cb2e49a8b25bc29da
//        timestamp：当前时间戳
//        sign：传值为md5加密(account+timestamp+秘钥)

        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
        String format = sdf.format(date);
        System.out.println(format);
        String compid = "SYZLCLY";
        String account = "RICHFUL";
        //接口名称
        String service = "syncBudgetDrafting";
        //传参方式
        String responseType = "2";
//        String clientip = "";
        String signType = "MD5";
        String key = "d597c54a85334f9cb2e49a8b25bc29da";
        String jms = account+format+key;
        String sign= "";
        try {
            // 创建MD5加密对象
            MessageDigest md = MessageDigest.getInstance("MD5");
            // 执行加密操作
            byte[] messageDigest = md.digest(jms.getBytes());
            // 将字节数组转换为16进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : messageDigest) {
                String hex = Integer.toHexString(0xff & b);
                if (hex.length() == 1) {
                    hexString.append('0');
                }
                hexString.append(hex);
            }
            // 返回加密后的字符串
//            return hexString.toString();
            sign = hexString.toString();
            System.out.println(sign);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }

        JSONObject param = new JSONObject();
        param.put("timestamp",format);
        param.put("compid",compid);
        param.put("account",account);
        param.put("service",service);
        param.put("responseType",responseType);
//        param.put("clientip",clientip);
        param.put("signType",signType);
        param.put("sign",sign);

        JSONObject params01 = new JSONObject();
        //法人公司
        params01.put("companyCode","1001");
        params01.put("profitCode","");
        //部门
        params01.put("budgetDeptCode","10011803");
        params01.put("projectCode","");
        params01.put("wbsCode","");
        params01.put("nbddCode","");
        //预算科目代码
        params01.put("budgetCode","53010101");
        //余额规则
        params01.put("balBalance","0");
        //预算周期1年度预算2月度预算3季度预算
        params01.put("budgetCycle","2");
        //年
        params01.put("year","2024");
        //月
        params01.put("months","10");
        //余额
        params01.put("budgetAmountMount","2000");
        JSONArray jsa = new JSONArray();
        jsa.add(params01);
        JSONObject budgetInfo = new JSONObject();
        budgetInfo.put("budgetInfo",jsa);
        param.put("data",budgetInfo);
        System.out.println(param);

        String s = OkHttpUtils.okHttpPost("http://fee-t.vetech.cn/fcopen/fcoutapi/syncBudgetDrafting", param.toString());
        System.out.println("返回结果: "+s);
    }
}
